<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
/***************************
傳進參數
GambleID：操盤序號
Bet_Type：下注種類
OddsID：賠率序號
Odds：賠率值
LottoID：樂透種類序號
Number：號碼
StakeMoney：下注金額
CheckKey：檢查鍵值(防止下注重複)
***************************/
include_once("../config.inc.php");

session_start();
$_DBSite = "Master";
$_LangPath = "wagerinsert";	//語言檔參數，務必放在最前面
include_once(__Root_Path."/fcommon/test.inc.php");
include_once(__DBConnect_File);
include_once(__Language_Path."/selectlanguage_member.php");
include_once(__Modules_Path."/Wager.lib.php");
include_once(__Modules_Path."/WagerForm.lib.php");
include_once(__Common_Path."/Lotto.inc.php");
include_once(__Common_Path."/ExchangeRate.inc.php");

$_data["LottoID"] = trim($_data["LottoID"]);
$_data["GambleID"] = trim($_data["GambleID"]);
$_data["Bet_Type"] = trim($_data["Bet_Type"]);
$_data["Number"] = trim($_data["Number"]);
$_data["OddsID"] = trim($_data["OddsID"]);
$_data["Odds"] = trim($_data["Odds"]);
$_data["StakeMoney"] = trim($_data["StakeMoney"]);
$_data["CheckKey1"] = trim($_data["CheckKey1"]);
$_data["CheckKey2"] = trim($_data["CheckKey2"]);
$flMemERate = (empty($_ECRate[$_SESSION["Member_Info"] -> ERID]["Rate"]))?1:$_ECRate[$_SESSION["Member_Info"] -> ERID]["Rate"];

//檢查關鍵參數是否為空值
if(strlen($_data["LottoID"]) == 0 || strlen($_data["Bet_Type"]) == 0 || strlen($_data["CheckKey2"]) == 0 || strlen($_data["CheckKey1"]) == 0 || strlen($_data["OddsID"]) == 0 || strlen($_data["StakeMoney"]) == 0 || strlen($_data["GambleID"]) == 0 || strlen($_data["Number"]) == 0){
	$Err_Title = $_strWagerInsert["BetError"];
	$Err_Message = $_strWagerInsert["BetAgain"];
	$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
	include("wager_error.php");
}
//檢查下注金額是否為數值
if(!preg_match("/^[0-9]{0,7}$/", $_data["StakeMoney"])){
	$Err_Title = $_strWagerInsert["BetError"];
	$Err_Message = $_strWagerInsert["StakeisString"];
	$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
	include("wager_error.php");
}
$_data["StakeMoney"] = intval($_data["StakeMoney"]);

//不正確下注模式
$_data["Bet_Type"] = intval($_data["Bet_Type"]);
if(!($_data["Bet_Type"] >= 1 && $_data["Bet_Type"] <=6)){
	$Err_Title = $_strWagerInsert["BetError"];
	$Err_Message = $_strWagerInsert["ErrBetType"];
	$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
	include("wager_error.php");
}

//會員、代理商、總代理、股東退水質不得為空值
$arrComm[1] = $_SESSION["Member_Info"] -> arrCommission[$_data["Bet_Type"]];			//會員退水值
$arrComm[2] = $_SESSION["Member_Info"] -> arrSubCommission[$_data["Bet_Type"]];		//代理商退水值
$arrComm[3] = $_SESSION["Member_Info"] -> arrMainCommission[$_data["Bet_Type"]];	//總代理退水值
$arrComm[4] = $_SESSION["Member_Info"] -> arrStockCommission[$_data["Bet_Type"]];	//股東退水值
if(empty($arrComm[1]) || empty($arrComm[2]) || empty($arrComm[3]) || empty($arrComm[4])){
	$Err_Title = $_strWagerInsert["BetError"];
	$Err_Message = $_strWagerInsert["CommissionErr"];
	$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
	include("wager_error.php");
}

include_once(__Common_Path."/limit/LottoLimit_".$_data["LottoID"].".inc.php");
include_once(__Common_Path."/betparam/BetParam_".$_data["LottoID"].".inc.php");
include_once(__Root_Path."/tmp/credit-".$_SESSION["Member_Info"] -> ID.".php");
if(!is_array($_arrBetParam[$_data["Bet_Type"]])){
	$Err_Message = "No Wager Relation Data!!";
	include("wager_error.php");
}
//===========================================================================//
//判斷最小單注或單場限額
$BetLimit = $_SESSION["Member_Info"] -> arrBetLimit[$_data["Bet_Type"]];
$EventLimit = $_SESSION["Member_Info"] -> arrEventLimit[$_data["Bet_Type"]];

if($_arrLottoLimit[$_data["Bet_Type"]][0] > 0){
	$_arrLottoLimit[$_data["Bet_Type"]][0] = floor($_arrLottoLimit[$_data["Bet_Type"]][0] / $flMemERate);
	if($_arrLottoLimit[$_ata["Bet_Type"]][0] < $EventLimit)$EventLimit = $_arrLottoLimit[$_data["Bet_Type"]][0];
}

if($_arrLottoLimit[$_data["Bet_Type"]][1] > 0){
	$_arrLottoLimit[$_data["Bet_Type"]][1] = floor($_arrLottoLimit[$_data["Bet_Type"]][1] / $flMemERate);
	if($_arrLottoLimit[$_data["Bet_Type"]][1] < $BetLimit)$BetLimit = $_arrLottoLimit[$_data["Bet_Type"]][1];
}
//公司單注限額
$Com_BetLimit = floor($_arrBetParam[$_data["Bet_Type"]]["Com_BetLimit"] / $_SESSION["Member_Info"] -> StockScale*$flMemERate);
if($Com_BetLimit < $BetLimit)$BetLimit = $Com_BetLimit;

$intMinStake = floor($_arrBetParam[$_data["Bet_Type"]]["MinStake"] / $flMemERate);
//下注金額不能小於最小下注金額
if($_data["StakeMoney"] < $intMinStake){
	$Err_Title = $_strWagerInsert["BetError"];
	$Err_Message = $_strWagerInsert["NotUnderMinStake"]."(".number_format($intMinStake).")";
	$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
	include("wager_error.php");
}
//下注金額不能大於單注限額
if($_data["StakeMoney"] > $BetLimit){
	$Err_Title = $_strWagerInsert["BetError"];
	$Err_Message = $_strWagerInsert["NotOverMaxStake"]."(".number_format($BetLimit).")";
	$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
	include("wager_error.php");
}
//下注金額不能大於單場限額
$intEvent = $_arrMemStake[$_data["Bet_Type"]][$_data["GambleID"]][$_data["Number"]];
if($_data["StakeMoney"] > ($EventLimit - $intEvent)){
	$Err_Title = $_strWagerInsert["BetError"];
	$Err_Message = $_strWagerInsert["NotOverEvenLimit"];
	$Err_Message .= $_strWagerInsert["EvenLimit"]."(".number_format($EventLimit).")<br>";
	$Err_Message .= $_strWagerInsert["ThisStake"]."(".number_format($intEvent).")";
	$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
	include("wager_error.php");
}
//下注金額不能大於信額額度
if($_data["StakeMoney"] > ($_SESSION["Member_Info"] -> CreditLine - $_arrMemStake["_SUM"])){
	$Err_Title = $_strWagerInsert["BetError"];
	$Err_Message = $_strWagerInsert["NotOverCredit"];
	$Err_Message .= $_strWagerInsert["MemCredit"]."(".number_format($_SESSION["Member_Info"] -> CreditLine).")<br>";
	$Err_Message .= $_strWagerInsert["UsedCredit"]."(".number_format($_arrMemStake["_SUM"]).")";
	$Err_HiddenMessage = "\nLine:".__LINE__.",\nPage:".__FILE__;
	include("wager_error.php");
}
//===========================================================================//
include_once(__Modules_Path."/RealBet.lib.php");
//是否上層停押
$strParentID = $_SESSION["Member_Info"] -> SubID.",".$_SESSION["Member_Info"] -> MainID.",".$_SESSION["Member_Info"] -> StockID;
if(!$strTop = GetOneStopWagerBet($_data["GambleID"], $_data["Bet_Type"], $_data["Number"], $strParentID)){
	$Err_Message = $GLOBALS["errormessage"];
	$Err_HiddenMessage = $GLOBALS["errorhidden"];
	include("wager_error.php");
}
if($strTop == "Stop"){
	$Err_Message = $_strWagerInsert["StopWager"];
	include("wager_error.php");
}
//===========================================================================//
//交易開始
PMA_mysql_query("BEGIN", $_ConnMasterMaster);
//取得最新號碼賠率資料
$arrGDate = getWagerArrayData($_data["LottoID"], $_data["GambleID"], $_data["Bet_Type"], $_data["Number"], $_data["OddsID"], $_data["Odds"]);
if($arrGDate["isError"]){
	PMA_mysql_query("ROLLBACK", $_ConnMasterMaster);
	$Err_Message = $_strWagerInsert[$arrGDate["Err_Message"]];
	include("wager_error.php");
}

$flOdds = floatval($arrGDate["Odds"]);
//加進分盤資料
switch($_data["Bet_Type"]){
	case 2:
	case 3:
	case 5:
	case 6:
		include_once(__Common_Path."/gambletype/GambleType".$_SESSION["Member_Info"] -> GambleType."_".$_data["LottoID"].".inc.php");
		$flOdds += $_arrGambleType[$arrGDate["Bet_Type"]];
		$flOdds += 1;
		break;
}
$strWagerConditions = getWagerConditions($arrGDate["Bet_Type"], $arrGDate["Number"], $flOdds, $arrGDate["SerialNo"]);

//組合陣列 for bet_BetListContent 用
$arrWCont["GameDate_x"] = $arrGDate["GameDate"];
$arrWCont["Content_x"] = $strWagerConditions;
$strCheckKey = str_replace("0.", ($_data["CheckKey2"] + j)."-", $_data["CheckKey1"]);
$arrWCont["checkkey_x"] = $strCheckKey;
$arrWCont["GambleType"] = $_SESSION["Member_Info"] -> GambleType;
$arrWCont["MemAccount_x"] = $_SESSION["Member_Info"] -> Account;
$arrWCont["isBlack"] = $_SESSION["Member_Info"] -> isBlack;

$flMemPayOff = ($_data["StakeMoney"]*($flOdds - 1));
//組合陣列 for bet_BetList 用
$arrWList["LottoID"] = $arrGDate["LottoID"];
$arrWList["Bet_Type"] = $arrGDate["Bet_Type"];
$arrWList["GambleID"] = $arrGDate["GambleID"];
$arrWList["Number"] = $arrGDate["Number"];
$arrWList["OddsID"] = $arrGDate["OddsID"];
$arrWList["Odds"] = $flOdds;
$arrWList["MemID"] = $_SESSION["Member_Info"] -> ID;
$arrWList["MemExchangeRate"] = $flMemERate;
$arrWList["MemCurrency"] = $_SESSION["Member_Info"] -> ERID;
$arrWList["MemStake"] = $_data["StakeMoney"];
$arrWList["MemPayOff"] = $flMemPayOff;
$arrWList["MemCommission"] = 1 - ($arrComm[1] / 100);
$arrWList["SubID"] = $_SESSION["Member_Info"] -> SubID;
$arrWList["SubCommission"] = 1 - ($arrComm[2] / 100);
$arrWList["SubRate"] = $_SESSION["Member_Info"] -> SubScale;
$arrWList["MainID"] = $_SESSION["Member_Info"] -> MainID;
$arrWList["MainCommission"] = 1 - ($arrComm[3] / 100);
$arrWList["MainRate"] = $_SESSION["Member_Info"] -> MainScale;
$arrWList["StockID"] = $_SESSION["Member_Info"] -> StockID;
$arrWList["StockCommission"] = 1 - ($arrComm[4] / 100);
$arrWList["StockRate"] = $_SESSION["Member_Info"] -> StockScale;
$arrWList["PayupDate_x"] = $arrGDate["PayupDate"];

//寫入注單
$arrBetData = InsertWagerToDB($arrWCont, $arrWList);
if($arrBetData["isError"]){
	PMA_mysql_query("ROLLBACK", $_ConnMasterMaster);
	$Err_Message = $arrBetData["ErrorMessage"];
	$Err_HiddenMessage = $arrBetData["ErrorHiddenMessage"];
	include("wager_error.php");
}

//押碼跳動
$arrJumpData = WagerJump($arrGDate, ($arrWList["MemStake"]*$arrWList["MemExchangeRate"]*$arrWList["StockRate"]), ($arrWList["MemPayOff"]*$arrWList["MemExchangeRate"]*$arrWList["StockRate"]), $_arrBetParam[$arrGDate["Bet_Type"]], $_arrGambleTypeAllOdds[$arrGDate["Bet_Type"]]);
if($arrJumpData["isError"]){
	PMA_mysql_query("ROLLBACK", $_ConnMasterMaster);
	$Err_Message = $arrBetData["ErrorMessage"].$GLOBALS["errormessage"];
	$Err_HiddenMessage = $arrBetData["ErrorHiddenMessage"].$GLOBALS["errorhidden"];
	include("wager_error.php");
}

$arrSucc[0]["BetID"] = $arrBetData["BetID"];
$arrSucc[0]["StakeMoneyValue"] = $_data["StakeMoney"];
$arrSucc[0]["WagerMessage"] = WagerFormTrans($arrGDate["LottoID"], $arrGDate["Bet_Type"], $strWagerConditions);
$arrSucc[0]["PayOffValue"] = $flMemPayOff;

//產生累積注額陣列
if(!getWagerArrayGold($_SESSION["Member_Info"] -> ID)){
	PMA_mysql_query("ROLLBACK", $_ConnMasterMaster);
	$Err_Message = $GLOBALS["errormessage"];
	$Err_HiddenMessage = $GLOBALS["errorhidden"];
	include("wager_error.php");
}
//交易完成
PMA_mysql_query("COMMIT", $_ConnMasterMaster);
$Err_Message = "Success!!";
include("wager_success.php");
?>